package com.OnTimeTeam.OnTime;

import java.util.ArrayList;

import com.OnTimeTeam.OnTime.Track.SubTrackBus;
import com.OnTimeTeam.OnTime.Track.SubTrackWalk;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;

import android.graphics.drawable.Drawable;

import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class BinderDataSubTracks extends BaseAdapter{

	private static final int TYPE_WALK = 0;
	private static final int TYPE_BUS = 1;
	private static final int TYPE_TOTAL = 2;
	private static final int TYPE_MAX_COUNT = 3;
	
	
	LayoutInflater inflater;
	ImageView thumb_image;
	Context context;
	
	Track track;
	ArrayList<Track.SubTrack> subTracks;
	ViewHolderWalk walk_holder;
	ViewHolderBus bus_holder;
	ViewHolderTotal total_holder;
	
	public BinderDataSubTracks(Activity act, ArrayList<Track.SubTrack> subTracks, Track track, Context ctx) 
	{
		context = ctx;
		this.track = track;
		this.subTracks = subTracks;
		
	/*	System.out.println("Printing from BinderDataSubTracks c'tor:\n");
		
		for(int i=0; i<subTracks.size(); i++)
			System.out.println(subTracks.get(i)+"\n");*/
		
		inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getItemViewType(int position){
		
	//	System.out.println("getItemView type of position #"+ position+ "\n");
		if(position == this.subTracks.size())
			return TYPE_TOTAL;
		else if(Track.SubTrackWalk.class.isInstance(this.subTracks.get(position)))
			return TYPE_WALK;
		else if(Track.SubTrackBus.class.isInstance(this.subTracks.get(position)))
			return TYPE_BUS;
		
		return 0;
		
	}
	
	 @Override
     public int getViewTypeCount() {
         return TYPE_MAX_COUNT;
     }
	 
	
	public int getCount() {
		// TODO Auto-generated method stub
		return (this.subTracks.size()+1);
	}

	
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		int type = this.getItemViewType(position);
		View vi = convertView;
		
		if(convertView == null)
		{ 
	      
	      switch(type){	
	      
	      case TYPE_WALK:  
	    	  
	    	  walk_holder = new ViewHolderWalk();
	    	  vi = inflater.inflate(R.layout.walk_item, null);
	    	  
	    	  walk_holder.tvDepName = (TextView)vi.findViewById(R.id.tvDepName);
	    	  walk_holder.tvArrName = (TextView)vi.findViewById(R.id.tvArrName);
	    	  walk_holder.tvDistance = (TextView)vi.findViewById(R.id.tvWalkDistance);
	    	  walk_holder.tvDuration = (TextView)vi.findViewById(R.id.tvWalkDuration);
	    	  walk_holder.ivWalk = (ImageView)vi.findViewById(R.id.ivWalk);
	    	 
	    	  vi.setTag(walk_holder);
	    	  break;
	     
	      case TYPE_BUS: 	  
	    	  
	    	  bus_holder = new ViewHolderBus();
	    	  vi = inflater.inflate(R.layout.bus_item, null);
	    	  
	    	  bus_holder.tvDepStop = (TextView)vi.findViewById(R.id.tvDepStop);
	    	  bus_holder.tvArrStop = (TextView)vi.findViewById(R.id.tvArrStop);
	    	  
	    	  bus_holder.tvVehicleType = (TextView)vi.findViewById(R.id.tvVehicleType);
	    	  bus_holder.tvLineCompanyName = (TextView)vi.findViewById(R.id.tvCompName);
	    	  bus_holder.tvLineName = (TextView)vi.findViewById(R.id.tvLineName);
	    	  bus_holder.tvLineShortName = (TextView)vi.findViewById(R.id.tvShortName);
	    	  
	    	  bus_holder.tvDuration = (TextView)vi.findViewById(R.id.tvBusDuration);
	    	  bus_holder.tvDepArrTime = (TextView)vi.findViewById(R.id.tvBusDepArrTime);
	    	  bus_holder.ivBus = (ImageView)vi.findViewById(R.id.ivBus);
	    	  
	    	  
	    	  vi.setTag(bus_holder);
	    	  break;
	    	  
	      case TYPE_TOTAL:  
	    	  
	    	  total_holder = new ViewHolderTotal();
	    	  vi = inflater.inflate(R.layout.total_item, null); 
	    	 
	    	  total_holder.tvEndName = (TextView)vi.findViewById(R.id.tvEndName);
	    	  total_holder.tvTotalDuration = (TextView)vi.findViewById(R.id.tvTotalDuration);
	    	  total_holder.tvTotalTime = (TextView)vi.findViewById(R.id.tvTotalTime);
	    	  //total_holder.ivDestination= (ImageView)vi.findViewById(R.id.ivDest);
	    	 
	    	  vi.setTag(total_holder);
	    	  break;
	     
	    	  
	      }
		}
		
		else
		{
		    if(type == TYPE_WALK)	
		    	walk_holder = (ViewHolderWalk)vi.getTag();
		   
		    //type == TYPE_BUS
		    else if(type == TYPE_BUS)
		    	bus_holder = (ViewHolderBus)vi.getTag();
		    
		    else if(type == TYPE_TOTAL)
		    	total_holder = (ViewHolderTotal)vi.getTag();
		 }
		
		String uri;
		int imageResource;
		Drawable image;
		   
		switch(type){	
	      
	      case TYPE_WALK:  

	    	  Track.SubTrackWalk subTrackWalk = (SubTrackWalk) subTracks.get(position);
	    	  walk_holder.tvDepName.setText(subTrackWalk.getStartPlaceFullName());
	    	 
	    	  if(position == 0 && track.getDepTime().equals("") == false) 
	    		 walk_holder.tvArrName.setText(context.getResources().getString(R.string.leave) + track.getDepTime() + ", " + subTrackWalk.getEndPlaceFullName());
	    	  else
	    		 walk_holder.tvArrName.setText(subTrackWalk.getEndPlaceFullName());
	    	 
	    	  walk_holder.tvDistance.setText("(" + subTrackWalk.getDistance()+ ")");
	    	  walk_holder.tvDuration.setText(context.getResources().getString(R.string.about) + " " + subTrackWalk.getDuration());
	    	  
	    	  //No need in dynamic image so might be unnecessary - remove it later
	    	  uri = "drawable/walk_transit";
	    	  imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier(uri, null, vi.getContext().getApplicationContext().getPackageName());
	  	      image = vi.getContext().getResources().getDrawable(imageResource);	
	    	  walk_holder.ivWalk.setImageDrawable(image);
	    	//  System.out.println("Set walk holder");
	    	  
	    	  break;
	     
	      case TYPE_BUS: 	  
	    	  
	    	  Track.SubTrackBus subTrackBus = (SubTrackBus) subTracks.get(position);
	    	     	  
	    	  bus_holder.tvDepStop.setText(subTrackBus.getDepStop());
	    	  bus_holder.tvArrStop.setText(context.getResources().getString(R.string.getOff) + subTrackBus.getArrStop());
	    	  
	    	  bus_holder.tvVehicleType.setText(subTrackBus.getVehicleType());
	    	  bus_holder.tvLineCompanyName.setText(subTrackBus.getLineOperatingCompanyName());
	    	  bus_holder.tvLineName.setText(context.getResources().getString(R.string.towards) +" "+ subTrackBus.getLineName());
	    	  
	    	  bus_holder.tvDepArrTime.setText(subTrackBus.getDepArrTime());
	    	  bus_holder.tvDuration.setText("(" + subTrackBus.getDuration()+")");
	    	  
	    	  //case subTrack is train
	    	  if(subTrackBus.getLineShortName().equals(""))
	    	  {
	    		  bus_holder.tvLineShortName.setText("");
	    		  uri = "drawable/train_transit";
	    	  }
	    	  else
	    	  {
	    		  bus_holder.tvLineShortName.setText(context.getResources().getString(R.string.line) +" "+ subTrackBus.getLineShortName());
	    		  uri = "drawable/bus_transit";
	    	  }
	    	  //No need in dynamic image so might be unnecessary - remove it later
	    	  imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier(uri, null, vi.getContext().getApplicationContext().getPackageName());
	  	      image = vi.getContext().getResources().getDrawable(imageResource);
	    	  bus_holder.ivBus.setImageDrawable(image);
	    	 
	    	 // System.out.println("Set bus holder");
	    	  
	    	  break;
	    	  
	      case TYPE_TOTAL:  
	    	  
	    	  total_holder.tvEndName.setText(track.getEndPlaceFullName());
	    	  total_holder.tvTotalDuration.setText(context.getResources().getString(R.string.totalDuration) +" "+ track.getTotalDuration());
	    	  total_holder.tvTotalTime.setText(track.getDepArrTime());
	    	 /*
	    	  uri = "drawable/location_place";
	    	  imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier(uri, null, vi.getContext().getApplicationContext().getPackageName());
	  	      image = vi.getContext().getResources().getDrawable(imageResource);
	    	  total_holder.ivDestination.setImageDrawable(image);*/
	    	 
	    	  break;
	     
		}
		
		
		return vi;
	}
	
	
	
	public static class ViewHolderWalk{
		
		TextView tvDepName;
		TextView tvArrName;
		
		TextView tvDistance;
		TextView tvDuration;
		
		ImageView ivWalk;
	}
	
	public static class ViewHolderBus{
		
		TextView tvArrStop;
		TextView tvDepStop;
		
		TextView tvVehicleType;
		TextView tvLineCompanyName;
		TextView tvLineName;
		TextView tvLineShortName;
		TextView tvDepArrTime;
		TextView tvDuration;
		
		ImageView ivBus;
		
	}
	
	public static class ViewHolderTotal{
		
		TextView tvEndName;	
		TextView tvTotalTime;
		TextView tvTotalDuration;
		
	}
	


}
